#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/wait.h>
#include <linux/sched.h>
#include <linux/interrupt.h>
#include <linux/fs.h>
#include <linux/of.h>
#include <linux/device.h>
#include <linux/platform_device.h>
#include <asm/uaccess.h>
#include <asm/io.h>

static wait_queue_head_t wq;

int  major = 0;
struct class *cls = NULL;
struct device *dev = NULL;

static int have_data = 0;
static int adc_data;
static struct resource *res1;
static struct resource *res2;
static void *adc_base;

#define ADCCON 0x0000
#define ADCDLY 0x0008
#define ADCDAT 0x000C
#define CLRINT 0x0018
#define ADCMUX 0x001C

int flags = 1;

#define DEBUG_PRINTK(msg,DEBUG_FLAG) \
    do{ \
        if(!!DEBUG_FLAG) { \
            printk("---->%s--->%d\n",__func__,__LINE__);\
            printk(msg);\
		}\
    }while(0)

static irqreturn_t adc_handler(int irqno, void *dev)
{
	have_data = 1;
	// printk("--->%s--->%d.\n",__func__,__LINE__);
	writel(0x12, adc_base + CLRINT);
	wake_up_interruptible(&wq);
	return IRQ_HANDLED;
}

int adc_driver_open(struct inode *pnode, struct file *pfile)
{
	printk("adc_driver_open is called\n");
	return 0;
}

int adc_driver_close(struct inode *pnode, struct file *pfile)
{
	printk("adc_driver_close is called\n");
	return 0;
}

static ssize_t adc_read(struct file *filep, char __user *buf, size_t len, loff_t *pos)
{
	writel(0x3, adc_base + ADCMUX);
	writel(1<<0|1<<14|1<<16|0xFF<<6, adc_base + ADCCON);
	wait_event_interruptible(wq, have_data == 1);

	adc_data = readl(adc_base + ADCDAT) & 0xFFF;

	if(copy_to_user(buf, &adc_data, sizeof(int)))
		return -EFAULT;
	have_data = 0;
	return len;
}

struct file_operations adc_ops = {
	.owner = THIS_MODULE,
	.open = adc_driver_open,
	.release = adc_driver_close,
	.read = adc_read
};

int adc_driver_probe(struct platform_device *pdev)
{
    // printk("--->%s--->%d.\n",__func__,__LINE__);
	int ret = -1;
	
	res1 = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
	res2 = platform_get_resource(pdev, IORESOURCE_MEM, 0);

	adc_base = ioremap(res2->start, res2->end - res2->start);
	printk("res2->start:%#x, res2->end - res2->start:%#x.\n", res2->start, res2->end - res2->start);

	ret = request_irq(res1->start, adc_handler, IRQF_DISABLED, "adc1", NULL);
	if(ret)
	{
		printk("request_irq failed\n");
		return -1;
	}
	printk("res1->start:%d.\n", res1->start);

	major = register_chrdev(0, "adcdriver", &adc_ops);
	if(major < 0)
	{
		printk("register_chrdev failed\n");
		return -1;
	}
	printk(KERN_INFO "major:%d\n", major);

	cls = class_create(THIS_MODULE, "fsadc");
	if(cls == NULL)
	{
		printk("class_create failed\n");
		return -1;
	}

	dev = device_create(cls, NULL, MKDEV(major, 0), NULL, "fsadc%d", 0);
	if(dev == NULL)
	{
		printk("device_create failed\n");
		return -1;
	}

	init_waitqueue_head(&wq);

	return 0;
}

int adc_driver_remove(struct platform_device *p_pltdev)
{
	free_irq(res1->start, NULL);
	iounmap(adc_base);
	device_destroy(cls, MKDEV(major, 0));
	class_destroy(cls);
	unregister_chrdev(major, "adcdriver");
	printk("adc_driver_remove is called\n");
	return 0;
}

struct of_device_id adc_dts[] = {
	{.compatible = "fs4412,adc3"},
	{}
};

struct platform_device_id adc_ids[] = {
	[0] = {"adc3", 0},
	[1] = {}
};

struct platform_driver adc_driver = {
	.driver = {
		.name = "adcdriver",
		.owner = THIS_MODULE,
		.of_match_table = adc_dts
	},
	.probe = adc_driver_probe,
	.remove = adc_driver_remove,

	.id_table = adc_ids
};
module_platform_driver(adc_driver);
MODULE_LICENSE("GPL");

